package thread;

public class SerialSum {
    //串行方式
    private static long sum = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            long ret = 0;
            for (long i = 1; i <= 10_0000_0000L ; i++) {
                ret += i;
            }
            sum += ret;
        });
        long beg = System.currentTimeMillis();
        t.start();
        t.join();
        long end = System.currentTimeMillis();
        System.out.println("sum= "+sum);
        System.out.println("程序执行时间为："+(end-beg)+"ms");
    }
}

class ConcurrentSum {
    //并发方式
    private static long sum = 0;
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            long ret = 0;
            for (long i = 1; i <= 5_0000_0000L ; i++) {
                ret += i;
            }
            sum += ret;
        });
        Thread t2 = new Thread(() -> {
            long ret = 0;
            for (long i = 5_0000_0001L; i <= 10_0000_0000L ; i++) {
                ret += i;
            }
            sum += ret;
        });
        long beg = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("sum= "+sum);
        System.out.println("程序执行时间为："+(end-beg)+"ms");
    }
}